package com.adds.lvds.service.impl.code;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.plugin.PageView;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.code.SysCodeMapper;
import com.adds.lvds.model.code.SysCode;
import com.adds.lvds.model.code.SysCodeType;

@Transactional
@Service("sysCodeService")
public class SysCodeService {
	@Autowired
	private SysCodeMapper sysCodeMapper;

	/**
	 * 录入代码值数据
	 * 
	 * @param code
	 * @throws Exception
	 */
	public String insertSysCode(SysCode code) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.insertSysCode(code);
			//缓存数据初始化
			SysCodeSetting.load();
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 编辑代码值类型
	 * 
	 * @param code
	 * @throws Exception
	 */
	public String updateSysCode(SysCode code) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.updateSysCode(code);
			//缓存数据初始化
			SysCodeSetting.load();
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除代码值数据
	 * 
	 * @param map
	 * @throws Exception
	 */
	public String deleteSysCode(Map<String, Object> map) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.deleteSysCode(map);
			//缓存数据初始化
			SysCodeSetting.load();
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据条件返回代码值数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public SysCode getSysCodeByMap(Map<String, Object> map) {
		SysCode sysCode = null;
		try {
			sysCode = sysCodeMapper.getSysCodeByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysCode;
	}

	/**
	 * 分页查询代码值数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public PageView<SysCode> querySysCode(PageView<SysCode> pageView, SysCode t) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("paging", pageView);
			map.put("typeName", t.getTypeName().trim());
			List<SysCode> list = sysCodeMapper.pageSysCodeByCondition(map);
			pageView.setRecords(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageView;
	}

	/**
	 * 录入代码类型数据
	 * 
	 * @param codeType
	 * @throws Exception
	 */
	public String insertCodeType(SysCodeType codeType) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.insertCodeType(codeType);
			SysCodeSetting.load();
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 编辑代码类型数据
	 * 
	 * @param codeType
	 * @throws Exception
	 */

	public String updateCodeType(SysCodeType codeType) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.updateCodeType(codeType);
			SysCodeSetting.load();
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除代码类型数据
	 * 
	 * @param typeName
	 * @throws Exception
	 */

	public String deleteCodeType(String typeName) {
		String result = CommUtils.SUCC;
		try {
			sysCodeMapper.deleteCodeType(typeName);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据条件获取代码类型
	 * 
	 * @param typeName
	 * @return
	 * @throws Exception
	 */
	public SysCodeType getCodeTypeByMap(String typeName) {
		SysCodeType sysCodeType=null;
		try {
			sysCodeType=sysCodeMapper.getCodeTypeByMap(typeName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysCodeType;
	}

	/**
	 * 分页查询代码类型
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public PageView<SysCodeType> querySysCodeType(PageView<SysCodeType> pageView, SysCodeType t) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("paging", pageView);
			map.put("t", t);
			List<SysCodeType> list = sysCodeMapper.pageSysCodeTypeByCondition(map);
			pageView.setRecords(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageView;
	}
}
